import { create } from "zustand"
import { immer } from "zustand/middleware/immer"

export type UserStore = {
  name: string;
  role: { role1: string; role2: string };
  updateRole: (value: string) => void;
}

// const useUserStore = create<UserStore>((set) => ({
//   name: "user",
//   role: { role1: "role1", role2: "role2" },
//   updateRole: () => set((state) => ({
//     role: {
//       ...state.role, // 不使用immer，需要手动合并状态
//       role1: "role11"
//     }
//   }))
// }))

const useUserStore = create<UserStore>()(
  immer((set) => ({
    name: "user",
    role: { role1: "role1", role2: "role2" },
    updateRole: (value) =>
      set((state) => {
        state.role.role1 = value
      }),
  }))
)

export default useUserStore